#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "mk_logger.h"
#include "../os/os_env.h"

#ifdef ANDROID

void android_log_msg(const char * szMessage, ...)
{
		char szText[2048] = {0};
		va_list paraList;
		va_start(paraList,szMessage);
	    vsnprintf(szText ,sizeof(szText), szMessage , paraList);
		va_end(paraList);

		FILE   *fp;
		char    timestr[32] = {0};

		if((fp=fopen("/sdcard/mk_log.log", "at"))==NULL)
			return;


		fprintf(fp, "\n%s", szText);
		fflush(fp);
		fclose(fp);

//		__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "%s",szText);
}

void android_log_buffer(unsigned char * pBuffer, long nBufLen , const char * szMessage, ...)
{
#define ONE_FRAME 16

	char szText[2048] = {0};
	va_list paraList;
	va_start(paraList,szMessage);
    vsnprintf(szText , sizeof(szText), szMessage , paraList);
	va_end(paraList);

	DBG_LOGMSG("%s", szText);

	if(pBuffer && nBufLen > 0)
	{
		long nCount = nBufLen / ONE_FRAME;
		long nLast = nBufLen % ONE_FRAME;
		char str[1024] = {0};
		long i = 0;

		for(i = 0 ; i < nCount ;i  ++)
		{
			memset(str,0, sizeof(str));
			for(int j=0;  j < ONE_FRAME ; j++)
			{
				char strNum[8]={0};
				sprintf(strNum, "0x%02x ", pBuffer[j + i * ONE_FRAME]);
				strcat(str, strNum);
			}

			DBG_LOGMSG("%s", str);
		}

		memset(str, 0, sizeof(str));
		for(int j=0;  j < nLast ; j++)
		{
			char strNum[8]={0};
			sprintf(strNum, "0x%02x ", pBuffer[j + i * ONE_FRAME]);
			strcat(str, strNum);
		}

		strcat(str,"\n");
		DBG_LOGMSG("%s", str);
	}
}
#endif


#define MK_LOG_DEFAULT_NAME "mk_log.log"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
char mk_logger::m_szLogFileName[260] = {0};


mk_logger::mk_logger()
{

}

mk_logger::~mk_logger()
{

}


void mk_logger::set_logfile( const char * logfile )
{
	strcpy(mk_logger::m_szLogFileName, logfile);
}

void mk_logger::init_log_path()
{
	if(strlen(m_szLogFileName) > 0)
		return ;
	
	char * tmpPath = os_get_temp_path();
	strcpy(mk_logger::m_szLogFileName, tmpPath);
	strcat(mk_logger::m_szLogFileName, MK_LOG_DEFAULT_NAME);
}

void mk_logger::remove_logfile()
{
	init_log_path();
	remove(m_szLogFileName);
}

int mk_logger::get_log_time(char *s)
{
	time_t	Time;
	struct tm *T;
	time(&Time);
	T = localtime(&Time);
	sprintf(s, "%02d%02d%02d %02d%02d%02d", T->tm_year-100, T->tm_mon + 1, T->tm_mday, T->tm_hour, T->tm_min, T->tm_sec);
	return(0);
}


void mk_logger::log_message(const char * szMessage, ...)
{

	init_log_path();

	char szText[2048] = {0};
	va_list paraList;
	va_start(paraList,szMessage);
    vsnprintf(szText , sizeof(szText), szMessage , paraList);
	va_end(paraList);

	FILE   *fp;
	char    timestr[32] = {0};

	get_log_time(timestr);

	unsigned long  error = os_get_last_error();

	if((fp=fopen(m_szLogFileName, "at"))==NULL)
		return;

	unsigned long threadId = 0;
	unsigned long processId = 0;

	threadId = os_get_current_thread_id();
	processId = os_get_current_process_id();

	fprintf(fp, "\n[%s][PID:%x, TID: %x]: %s", timestr, processId, threadId, szText);
	fflush(fp);
	fclose(fp);

	os_set_last_error(error);

	return;
}

#ifdef WIN32
#include <windows.h>
void mk_logger::log_message2(const char * szMessage, ...)
{
	char szText[2048] = {0};
	va_list paraList;
	va_start(paraList,szMessage);
	vsnprintf(szText , sizeof(szText), szMessage , paraList);
	va_end(paraList);

	OutputDebugStringA(szText);

	return;
}
#endif

void mk_logger::log_buffer(unsigned char * pBuffer, long nBufLen , const char * szMessage, ...)
{

#define ONE_FRAME 16

	char szText[2048] = {0};
	va_list paraList;
	va_start(paraList,szMessage);
    vsprintf(szText , szMessage , paraList);
	va_end(paraList);

	DBG_LOGMSG(szText);

	if(pBuffer && nBufLen > 0)
	{
		long nCount = nBufLen / ONE_FRAME;
		long nLast = nBufLen % ONE_FRAME;
		char str[1024] = {0};
		long i = 0;

		for(i = 0 ; i < nCount ;i  ++)
		{
			memset(str,0, sizeof(str));
			for(int j=0;  j < ONE_FRAME ; j++)
			{
				char strNum[8]={0};
				sprintf(strNum, "0x%02x ", pBuffer[j + i * ONE_FRAME]);
				strcat(str, strNum);
			}

			DBG_LOGMSG(str);
		}

		memset(str, 0, sizeof(str));
		for(int j=0;  j < nLast ; j++)
		{
			char strNum[8]={0};
			sprintf(strNum, "0x%02x ", pBuffer[j + i * ONE_FRAME]);
			strcat(str, strNum);
		}

		strcat(str,"\n");
		DBG_LOGMSG(str);

	}
}
